﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using School.Entities;
using School.Common;
using System.Data.Entity;
using School.Admission;
using School.Organization ;


namespace School.Pages.Admission
{
    public partial class StuffPages : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1016) == true)
                {

                    if (!(Page.IsPostBack))
                    {
                        //security

                        //fill crls
                        Fillcrls();
                        InitializeControls();

                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");

            }
            else
                Response.Redirect("../organization/no_privelege.aspx");

        }


        # region "Fill all crls"

        //***************** This is function to fill crl  ***************************
        //-**************************************************************************

        protected void Fillcrls()
        {

            ddlUser_Id.DataSource = Staff.All().Select(x => new { UserFullName = x.FName + " " + x.MName + " " + x.LName, Id = x.Id });
            ddlUser_Id.DataTextField = "UserFullName";
            ddlUser_Id.DataValueField = "Id";
            ddlUser_Id.DataBind();
            ddlUser_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
            
            gridPages.DataSource =  StuffPagesCls.AllPages();
            gridPages.DataBind();

            gridPages.Visible = true;
        }


        //***************** This is function to initlaize crl  ***************************
        //-**************************************************************************
        protected void InitializeControls()
        {
            

        }

        # endregion


        # region "Mode Handlers"

        //***************** This is function to handle page in add mode  ***************************
        //-**************************************************************************
        protected void PutFormInAddMode()
        {
           
        }


        //***************** This is function to handle page in brows mode  ***************************
        //-**************************************************************************

        protected void PutFormInBrwosMode()
        {
            pnlAdd.Visible = false;

        }

        #endregion






        # region   "Execute commands sent to the form"



        //***************** This is function to handle page when Puser click in الغاء btn  ***************************
        //-**************************************************************************
        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }


        //***************** This is function to handle page when Puser click in اضافة btn  ***************************
        //-**************************************************************************

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            pnlAdd.Visible = true;
        }

        //***************** This is function to handle page when Puser click in انهاء btn  ***************************
        //-**************************************************************************


        protected void btnend_Click(object sender, EventArgs e)
        {
            pnlAdd.Visible = false;
        }
        //***************** This is function to handle page when Puser click in حفظ btn  ***************************
        //-**************************************************************************

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Int64 Stuff_Id = Convert.ToInt64(ddlUser_Id.SelectedValue);
            int i;
            if (StuffPagesCls.DeleteByStuffID(Stuff_Id) == true)
            {
            

                for (i = 0; (i <= (gridPages.Rows.Count - 1)); i++)
                {
                 
               
                    if (gridPages.Rows[i].Visible == true)
                    {
                        CheckBox AcceptStudent;
                        AcceptStudent = ((CheckBox)(gridPages.Rows[i].FindControl("CheckBox1")));

                        if ((AcceptStudent.Checked == true))
                        {
                            Int64 trid;
                            Label lbltrid;
                            lbltrid = ((Label)(gridPages.Rows[i].FindControl("lbltrid")));
                            trid = Convert.ToInt64(lbltrid.Text);
                            if (StuffPagesCls.Add(new ORG_Stuff_Pages
                            {
                                Stuff_Id = Stuff_Id,
                                Page_Id = trid
                            }) != -1)
                            {
                                lblMessageAdd.Text = "تم ادخال البيانات بنجاح";
                            }

                        }
                    }
              }
            }
            else
            {
                lblMessageAdd.Text = "خطأ في ادخال البيانات";
            }
            
            
        }


        # endregion



        # region " Search *********************"









        # endregion

        protected void ddlCertificate_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        

        protected void btnback_Click(object sender, EventArgs e)
        {
            
        }

        protected void ddlUser_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i;
            Int64 Stuff_Id = Convert.ToInt64(ddlUser_Id.SelectedValue);
            for (i = 0; (i <= (gridPages.Rows.Count - 1)); i++)
            {

                if (gridPages.Rows[i].Visible == true)
                {
                    CheckBox AcceptStudent;
                    AcceptStudent = ((CheckBox)(gridPages.Rows[i].FindControl("CheckBox1")));


                        Int64 trid;
                        Label lbltrid;
                        lbltrid = ((Label)(gridPages.Rows[i].FindControl("lbltrid")));
                        trid = Convert.ToInt64(lbltrid.Text);
                        if (StuffPagesCls.Search(Stuff_Id, trid) != -1)
                        {
                            AcceptStudent.Checked  = true;
                        }
                        else
                        {
                            AcceptStudent.Checked = false;
                        }
                }
            }
        }

        protected void gridPages_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void gridPages_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void gridPages_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void ddlSearchUser_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

        }



    }
}